<?php

namespace App\Services;

use App\Common\Helper;
use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\User;
use App\Services\Dao\UserDao;
use Hyperf\Di\Annotation\Inject;

class UserService
{

    /**
     * @Inject
     * @var UserDao
     */
    protected $userDao;

    public function register(array $params): UserAuth
    {
        $model = $this->userDao->findByUserName($params['username']);
        if (!empty($model)) {
            throw new BusinessException(ErrorCode::USER_IS_EXIST, ErrorCode::getMessage(ErrorCode::USER_IS_EXIST));
        }
        $model = new User();
        $model->username = $params['username'];
        $model->salt = Helper::instance()->getRandomWordCharacters(8);
        $model->password = Helper::instance()->md5Pwd($params['password'], $model->salt);
        $model->phone_iv = Helper::instance()->getRandomWordCharacters(16);
        $model->phone = Helper::instance()->encryptCbcStr($params['phone'] ?? '', $model->phone_iv);
        $model->id_number_iv = Helper::instance()->getRandomWordCharacters(16);
        $model->id_number = Helper::instance()->encryptCbcStr($params['idNumber'] ?? '', $model->id_number_iv);
        $model->save();

        return UserAuth::instance()->init($model);
    }

    public function login(array $params): UserAuth
    {
        if ($params['loginType'] == 1) {
            //账号密码登录
            $model = $this->userDao->findByUserName($params['username']);
            if (empty($model)) {
                throw new BusinessException(ErrorCode::USERNAME_OR_PWD_ERROR, ErrorCode::getMessage(ErrorCode::USERNAME_OR_PWD_ERROR));
            }
            $pwd = Helper::instance()->md5Pwd($params['password'], $model->salt);
            if (!$model->verifyPwd($pwd, $model->password)) {
                throw new BusinessException(ErrorCode::USERNAME_OR_PWD_ERROR, ErrorCode::getMessage(ErrorCode::USERNAME_OR_PWD_ERROR));
            }
        } elseif ($params['loginType'] == 2) {
            //手机号获取验证码登录
            if (!$this->verifyCode()) {
                throw new BusinessException(ErrorCode::MSG_CODE_ERROR, ErrorCode::getMessage(ErrorCode::MSG_CODE_ERROR));
            }
            $model = $this->userDao->findByPhone($params['phone']);
            if (empty($model)) {
                throw new BusinessException(ErrorCode::USER_NOT_EXIST, ErrorCode::getMessage(ErrorCode::USER_NOT_EXIST));
            }
        } else {
            throw new BusinessException(ErrorCode::PARAMS_ERROR, ErrorCode::getMessage(ErrorCode::PARAMS_ERROR));
        }
        return UserAuth::instance()->init($model);
    }

    public function getUserInfo($userId)
    {
        return $this->userDao->first($userId, true);
    }

    public function save(int $userId, array $data)
    {
        $model = $this->userDao->first($userId, true);
        $model->nickname = $data['nickname'];
        $model->header_image = $data['headerImage'] ?? '';
        $model->email = $data['email'] ?? '';
        $model->wechat_code = $data['wechatCode'] ?? '';
        $model->gender = $data['gender'] ?? 0;
        $model->profession = $data['profession'] ?? 0;
        $model->phone = Helper::instance()->encryptCbcStr($data['phone'] ?? '', $model->phone_iv);

        return $model->save();
    }

    /**
     * TODO:发送验证码
     */
    public function sendMsgCode(){}

    /**
     * TODO:校验验证码
     */
    public function verifyCode(): bool
    {
        return true;
    }

}